package com.csu.device.web;

import com.csu.device.entity.SiteDO;
import com.csu.device.service.SiteService;

import com.csu.util.PageUtils;
import com.csu.util.Query;
import com.csu.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 位置
 */
@Controller
@RequestMapping("/site")
public class SiteController {
    @Autowired
    private SiteService siteService;

    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<SiteDO> siteList = siteService.list(query);
        int total = siteService.count(query);
        PageUtils pageUtils = new PageUtils(siteList, total);
        return pageUtils;
    }

    @GetMapping("/getSite/{id}")
    @ResponseBody
    SiteDO getSite(@PathVariable("id") Integer id, Model model) {

        return siteService.get(id);
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    public R save(SiteDO site) {
        if (siteService.save(site) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public R update(SiteDO site) {
        siteService.update(site);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    public R remove(Integer id) {
        if (siteService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        siteService.batchRemove(ids);
        return R.ok();
    }

}
